package net.gylka.newsreader;

import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;

public abstract class BaseNewsReaderActivity extends FragmentActivity implements ViewModeChangeListener {

	public final static String PACKAGE_NAME = "net.gylka.newsreader";

	protected NewsReaderSettings mNewsReaderSettings;
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.menu, menu);
		return true;
	}

	@Override
	protected void onCreate(Bundle arg0) {
		// Initializing  settings
		// TODO : Add saving and loading of settings and remove initialization 
		this.mNewsReaderSettings = new NewsReaderSettings();
		this.mNewsReaderSettings.articlesViewMode = NewsReaderSettings.VIEW_MODE_PLAIN_TEXT;
		super.onCreate(arg0);
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		MenuItem viewModeMenuItem = (MenuItem)menu.findItem(R.id.action_view_mode);
		if (mNewsReaderSettings.articlesViewMode == NewsReaderSettings.VIEW_MODE_HTML)
			viewModeMenuItem.setTitle(R.string.action_view_mode_html);
		if (mNewsReaderSettings.articlesViewMode == NewsReaderSettings.VIEW_MODE_PLAIN_TEXT)
			viewModeMenuItem.setTitle(R.string.action_view_mode_plain_text);
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case R.id.action_view_mode :
				ViewModeDialogFragment viewModeDialog = ViewModeDialogFragment.newInstance(mNewsReaderSettings.articlesViewMode);
				viewModeDialog.show(this.getSupportFragmentManager(), "dialog");
				return true;
			default:
				return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public void onViewModeChanged(int viewModeSetting) {
		mNewsReaderSettings.articlesViewMode = viewModeSetting;
		ActivityCompat.invalidateOptionsMenu(this);
	}

}
